package com.allwees.bs.c.module.user.req;


import com.allwees.bs.c.module.user.entity.ShippingAddressEntity;
import com.allwees.bs.core.modelbase.constant.ErrorMessage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ShippingAddressReq implements Serializable {

    //是否是默认收货地址
    protected Boolean isDefault;

    //名
    @NotBlank(message = ErrorMessage.PARAM_NOT_NULL)
    @Length(min = 1, max = 16, message = "FirstName's length not valid~")
    protected String firstName;

    //姓
    @NotBlank(message = ErrorMessage.PARAM_NOT_NULL)
    @Length(min = 1, max = 16, message = "LastName's length not valid~")
    protected String lastName;

    //国际电话区号
//    @NotBlank(message = ErrorMessage.PARAM_NOT_NULL)
    @Length(min = 1, max = 10, message = ErrorMessage.PARAM_LENGTH_ERROR)
    protected String itu;

    //电话号码
    @NotBlank(message = ErrorMessage.PARAM_NOT_NULL)
    @Length(min = 2, max = 16, message = "Phone's length not valid~")
    protected String phone;

    //国家
    @NotBlank(message = ErrorMessage.PARAM_NOT_NULL)
    @Length(min = 1, max = 50, message = ErrorMessage.PARAM_LENGTH_ERROR)
    protected String country;

    //州/省
    @NotBlank(message = ErrorMessage.PARAM_NOT_NULL)
    @Length(min = 1, max = 50, message = ErrorMessage.PARAM_LENGTH_ERROR)
    protected String province;

    //城市
    @NotBlank(message = ErrorMessage.PARAM_NOT_NULL)
    @Length(min = 1, max = 50, message = "City's length not valid~")
    protected String city;

    //区号
    @Length(min = 2, max = 16, message = "ZipCode's length not valid~")
    protected String zipCode;


    //地区码
    @NotBlank(message = ErrorMessage.PARAM_NOT_NULL)
    @Length(min = 2, max = 16, message = "district's length not valid~")
    protected String district;

    //地址第一行
    @NotBlank(message = ErrorMessage.PARAM_NOT_NULL)
    @Length(min = 2, max = 255, message = "AddressLine1's length not valid~")
    protected String addressLine1;

    //地址第二行,可为空
    protected String addressLine2;

    protected String note;

    public boolean getIsDefault() {
        return this.isDefault != null && this.isDefault;
    }

    public ShippingAddressEntity map2Entity() {
        return map2Entity(new ShippingAddressEntity());
    }

    public ShippingAddressEntity map2Entity(ShippingAddressEntity entity) {
        //1表示默认，0表示非默认
        entity.setIsDefault(this.getIsDefault() ? 1 : 0);
        entity.setFirstName(this.getFirstName());
        entity.setLastName(this.getLastName());
        entity.setItu(this.getItu());
        entity.setPhone(this.getPhone());
        entity.setCountryId(Integer.parseInt(this.getCountry()));
        entity.setProvinceId(Integer.parseInt(this.getProvince()));
        entity.setCityId(Integer.parseInt(this.getCity()));
        entity.setDistrictId(Integer.parseInt(this.getDistrict()));
        entity.setZipCode(this.getZipCode());

        entity.setAddressLine1(this.getAddressLine1());
        entity.setAddressLine2(this.getAddressLine2());
        entity.setNote(this.getNote());
        return entity;
    }
}
